#coding=utf-8
#Boa:Dialog:DlgCustomerLabel

import wx
import wx.lib.buttons

from product import CDataCustomerLabeling, CDataCustomerLabel

def create(parent):
    return DlgCustomerLabel(parent)

[wxID_DLGCUSTOMERLABEL, wxID_DLGCUSTOMERLABELBTNCANCEL, 
 wxID_DLGCUSTOMERLABELBTNCONFIRM, wxID_DLGCUSTOMERLABELBTNNEW, 
 wxID_DLGCUSTOMERLABELCHECKLISTBOX, 
] = [wx.NewId() for _init_ctrls in range(5)]

class DlgCustomerLabel(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGCUSTOMERLABEL,
              name=u'DlgCustomerLabel', parent=prnt, pos=wx.Point(617, 181),
              size=wx.Size(328, 436), style=wx.DEFAULT_DIALOG_STYLE,
              title=u'\u5ba2\u6237\u5173\u952e\u5b57')
        self.SetClientSize(wx.Size(320, 402))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.Center(wx.BOTH)

        self.btnNew = wx.Button(id=wxID_DLGCUSTOMERLABELBTNNEW,
              label=u'\u65b0\u5efa', name=u'btnNew', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(86, 29), style=0)
        self.btnNew.Bind(wx.EVT_BUTTON, self.OnBtnNewButton,
              id=wxID_DLGCUSTOMERLABELBTNNEW)

        self.btnConfirm = wx.Button(id=wxID_DLGCUSTOMERLABELBTNCONFIRM,
              label=u'\u786e\u8ba4', name=u'btnConfirm', parent=self,
              pos=wx.Point(144, 0), size=wx.Size(86, 29), style=0)
        self.btnConfirm.Bind(wx.EVT_BUTTON, self.OnBtnConfirmButton,
              id=wxID_DLGCUSTOMERLABELBTNCONFIRM)

        self.btnCancel = wx.Button(id=wxID_DLGCUSTOMERLABELBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(232, 0), size=wx.Size(86, 29), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGCUSTOMERLABELBTNCANCEL)

        self.checkListBox = wx.CheckListBox(choices=[],
              id=wxID_DLGCUSTOMERLABELCHECKLISTBOX, name=u'checkListBox',
              parent=self, pos=wx.Point(0, 32), size=wx.Size(320, 368),
              style=0)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        self.choices = CDataCustomerLabel.getNames()
        self.checkListBox.AppendItems(self.choices)

    def setEditData(self, sels):
        for index in range(self.checkListBox.GetCount()):
            str = self.checkListBox.GetString(index)
            if(str in sels):
                self.checkListBox.Check(index)
    
    def getSelected(self):
        return self.m_sel
        
    def OnBtnNewButton(self, event):
        dlg = wx.TextEntryDialog(
                self, u'请输入新的关键字名称，请注意避免重复：',
                '新建客户关键字', '')

        if dlg.ShowModal() == wx.ID_OK:
            val = dlg.GetValue()
            if val in self.choices:
                dlg = wx.MessageDialog(self, u'错误输入：名字发生重复', u'提示',
                                    wx.OK|wx.ICON_ERROR)
                dlg.ShowModal()
                return
            
            CDataCustomerLabel.add(val)
            self.choices.append(val)
            self.checkListBox.Append(val)

    def OnBtnConfirmButton(self, event):
        self.m_sel = []
        #selections = self.checkListBox.GetSelections()
        #print selections
        #for index in selections:
            #self.m_sel.append(self.checkListBox.GetString(index))

        for index in range(self.checkListBox.GetCount()):
            if (self.checkListBox.IsChecked(index)):
                self.m_sel.append(self.checkListBox.GetString(index))
            
        self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)
